home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7136 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: calloc help needed
  5. Date: Fri, 16 Feb 96 20:47:41 GMT
  6. Organization: none
  7. Message-ID: <824503661snz@genesis.demon.co.uk>
  8. References: <1996Feb15.125431.7751@leeds.ac.uk> <4fvmgbINNbp0@keats.ugrad.cs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4fvmgbINNbp0@keats.ugrad.cs.ubc.ca>
  15.            c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
  16.  
  17. >In article <1996Feb15.125431.7751@leeds.ac.uk>,
  18. >A M Casey <csyamc@scs.leeds.ac.uk> wrote:
  19. > >I'm trying to allocate enough memory for an array of strings, defined as
  20. > >char * MyArray[20].
  21.  
  22. That defines an array of 20 pointers. You will have to set each pointer
  23. individually to point into a suitable array of characters.
  24.  
  25. > >the only manual entry I have for calloc says that it needs two values, the
  26. > >number of elements, and the size of each, but how do I actually use it?
  27. > >
  28. > >MyArray = calloc(20,50);
  29.  
  30. This is illegal with the definition of MyArray you have (you can't assign
  31. to an array). What you may want is:
  32.  
  33.    char (*MyArray)[50];
  34.  
  35. where I take 50 to be the wize of each character array to be allocated for
  36. each string. However see the FAQ for more information about allocating
  37. '2D' arrays.
  38.  
  39. >This is gives you much more space than you need for the array. A character's
  40. >size is not 50!
  41.  
  42. But he asked for an array of 'strings' not an array of characters.
  43.  
  44. > >doesnt work, as far as I can tell calloc returns an int.
  45.  
  46. ANSI calloc returns void *.
  47.  
  48. >It is probably "implicitly defined" to return an int, because you lack a
  49. >declaration. Try including the "malloc.h" header file.
  50.  
  51. The correct header file to include is stdlib.h.
  52.  
  53. -- 
  54. -----------------------------------------
  55. Lawrence Kirby | fred@genesis.demon.co.uk
  56. Wilts, England | 70734.126@compuserve.com
  57. -----------------------------------------
  58.